/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jpa.controller;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import jpa.controller.exceptions.IllegalOrphanException;
import jpa.controller.exceptions.NonexistentEntityException;
import jpa.controller.exceptions.PreexistingEntityException;
import jpa.entities.Cuotas;
import jpa.entities.Tarjeta;
import java.util.ArrayList;

/**
 *
 * @author Usuario
 */
public class CuotasJpaController {

    public CuotasJpaController() {
        emf = Persistence.createEntityManagerFactory("Sig-ejbPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Cuotas cuotas) throws IllegalOrphanException, PreexistingEntityException, Exception {
        List<String> illegalOrphanMessages = null;
        Tarjeta tarjetaOrphanCheck = cuotas.getTarjeta();
        if (tarjetaOrphanCheck != null) {
            Cuotas oldCuotasOfTarjeta = tarjetaOrphanCheck.getCuotas();
            if (oldCuotasOfTarjeta != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Tarjeta " + tarjetaOrphanCheck + " already has an item of type Cuotas whose tarjeta column cannot be null. Please make another selection for the tarjeta field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tarjeta tarjeta = cuotas.getTarjeta();
            if (tarjeta != null) {
                tarjeta = em.getReference(tarjeta.getClass(), tarjeta.getIdTarjeta());
                cuotas.setTarjeta(tarjeta);
            }
            em.persist(cuotas);
            if (tarjeta != null) {
                tarjeta.setCuotas(cuotas);
                tarjeta = em.merge(tarjeta);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCuotas(cuotas.getIdCuotas()) != null) {
                throw new PreexistingEntityException("Cuotas " + cuotas + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Cuotas cuotas) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cuotas persistentCuotas = em.find(Cuotas.class, cuotas.getIdCuotas());
            Tarjeta tarjetaOld = persistentCuotas.getTarjeta();
            Tarjeta tarjetaNew = cuotas.getTarjeta();
            List<String> illegalOrphanMessages = null;
            if (tarjetaNew != null && !tarjetaNew.equals(tarjetaOld)) {
                Cuotas oldCuotasOfTarjeta = tarjetaNew.getCuotas();
                if (oldCuotasOfTarjeta != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Tarjeta " + tarjetaNew + " already has an item of type Cuotas whose tarjeta column cannot be null. Please make another selection for the tarjeta field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (tarjetaNew != null) {
                tarjetaNew = em.getReference(tarjetaNew.getClass(), tarjetaNew.getIdTarjeta());
                cuotas.setTarjeta(tarjetaNew);
            }
            cuotas = em.merge(cuotas);
            if (tarjetaOld != null && !tarjetaOld.equals(tarjetaNew)) {
                tarjetaOld.setCuotas(null);
                tarjetaOld = em.merge(tarjetaOld);
            }
            if (tarjetaNew != null && !tarjetaNew.equals(tarjetaOld)) {
                tarjetaNew.setCuotas(cuotas);
                tarjetaNew = em.merge(tarjetaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cuotas.getIdCuotas();
                if (findCuotas(id) == null) {
                    throw new NonexistentEntityException("The cuotas with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cuotas cuotas;
            try {
                cuotas = em.getReference(Cuotas.class, id);
                cuotas.getIdCuotas();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cuotas with id " + id + " no longer exists.", enfe);
            }
            Tarjeta tarjeta = cuotas.getTarjeta();
            if (tarjeta != null) {
                tarjeta.setCuotas(null);
                tarjeta = em.merge(tarjeta);
            }
            em.remove(cuotas);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Cuotas> findCuotasEntities() {
        return findCuotasEntities(true, -1, -1);
    }

    public List<Cuotas> findCuotasEntities(int maxResults, int firstResult) {
        return findCuotasEntities(false, maxResults, firstResult);
    }

    private List<Cuotas> findCuotasEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Cuotas.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Cuotas findCuotas(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Cuotas.class, id);
        } finally {
            em.close();
        }
    }

    public int getCuotasCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Cuotas> rt = cq.from(Cuotas.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
